﻿using Corina.DataContext.Service;
using Corina.Datamodel.DataObject;
using Corina.Datamodel.Entities;
using Corina.Enumerations;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace Corina.DataContext.Master
{
    public class ManagerMaster : MasterBase
    {
        public ResultInfo Login(string account, string password)
        {
            ResultValue value = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(value);

            if (value == ResultValue.Succeed)
            {
                ManagerInfo manager = GetAccountInfo<ManagerInfo>(account);

                RoleInfo role = GetManagerRole(manager.RoleId);

                AvatarInfo avatar = GetAccountAvatar(manager.Id, AccountTypeValue.Manager);

                result.Insert(manager, role, avatar);
            }

            result.Result = value;

            return result;
        }

        /// <summary>
        /// 更改账户信息
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="entity">被更改或新增的账户实体</param>
        /// <param name="operation">操作类型：添加、删除、修改</param>
        /// <returns></returns>
        public ResultInfo Update(string account, string password, ManagerInfo entity, EntityOperation operation)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                ManagerInfo existAccount = GetAccountInfo<ManagerInfo>(entity.Account);
                if (operation == EntityOperation.Insert && existAccount != null)
                {
                    resultValue = ResultValue.Exists;
                }
                else
                {
                    bool flag = GetInstance<ManagerService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, operation);
                    if (!flag)
                    {
                        resultValue = ResultValue.Failed;
                    }
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        public ResultInfo Update(string account, string password, string name, string mobile, string email)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                ManagerInfo entity = GetAccountInfo<ManagerInfo>(account);
                entity.Name = name;
                entity.Mobile = mobile;
                entity.Email = email;
                bool flag = GetInstance<ManagerService>().Update(ConnectionString(DatabaseKeys.CORINA_ACCO), entity, EntityOperation.Update);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        /// <summary>
        /// 更改密码
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="newpassword"></param>
        /// <returns></returns>
        public ResultInfo UpdatePassword(string account, string password, string newpassword)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<ManagerService>().UpdatePassword(ConnectionString(DatabaseKeys.CORINA_ACCO), account, newpassword);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="account">验证帐号</param>
        /// <param name="password">密码</param>
        /// <param name="accountId">被更改账户Id</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public ResultInfo UpdateStatus(string account, string password, int accountId, AccountStatusValue status)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<ManagerService>().UpdateStatus(ConnectionString(DatabaseKeys.CORINA_ACCO), accountId, status);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        /// <summary>
        /// 更改账户角色
        /// </summary>
        /// <param name="account"></param>
        /// <param name="password"></param>
        /// <param name="accountId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public ResultInfo UpdateRole(string account, string password, int accountId, int roleId)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            if (resultValue == ResultValue.Succeed)
            {
                bool flag = GetInstance<ManagerService>().UpdateRole(ConnectionString(DatabaseKeys.CORINA_ACCO), accountId, roleId);
                if (!flag)
                {
                    resultValue = ResultValue.Failed;
                }
            }

            ResultInfo result = new ResultInfo(resultValue);

            return result;
        }

        public ResultInfo GetEntity(string account,string password,int id)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                var item = GetAccountInfo<ManagerInfo>(id);

                result.Entities.Add(item);

            }

            return result;
        }

        public ResultInfo GetAllItems(string account, string password)
        {
            ResultValue resultValue = VerifyAccount<ManagerInfo>(account, password);

            ResultInfo result = new ResultInfo(resultValue);

            if (resultValue == ResultValue.Succeed)
            {
                DataTable table = GetInstance<ManagerService>().GetAllItems(ConnectionString(DatabaseKeys.CORINA_ACCO));

                string[] columns = table.Columns.OfType<DataColumn>().Select(t => t.ColumnName).ToArray();

                var items = (from t in table.AsEnumerable()
                             select new DataObjectInfo()
                             {
                                 Values = GetObjectOfValue(t, columns)
                             });

                result.DataObjects.AddRange(items);
                
            }

            return result;
        }
    }
}
